package com.adwin.addcoin.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map


//定义DataStore
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "user_info")


//定义key
val keyToken = stringPreferencesKey("token")
val keyRepostNumber = intPreferencesKey("repost")
val agreementKey = booleanPreferencesKey("agreement")
val userGaid = stringPreferencesKey("gaid")
val devicesId = stringPreferencesKey("devicesId")
val offerTask = stringPreferencesKey("offerTask")

suspend fun DataStore<Preferences>.saveTaskArray(jsonstring:String){
    this.edit {
        it[offerTask] = jsonstring
    }
}

suspend fun Context.getTaskArray():String{
    val tokenFlow :Flow<String> =  this.dataStore.data.map {
        it[offerTask]?:"[]"
    }
    return tokenFlow.first()
}

suspend fun DataStore<Preferences>.saveDevicesId(devices:String){
    this.edit {
        it[devicesId] = devices
    }
}

suspend fun Context.getDevicesId():String{
    val tokenFlow :Flow<String> =  this.dataStore.data.map {
        it[devicesId]?:""
    }
    return tokenFlow.first()
}

suspend fun DataStore<Preferences>.saveUserGaid(gaid:String){
    this.edit {
        it[userGaid] = gaid
    }
}

suspend fun Context.getUserGaid():String{
    val tokenFlow :Flow<String> =  this.dataStore.data.map {
        it[userGaid]?:""
    }
    val gaid = tokenFlow.first()
    return if (TextUtils.isEmpty(gaid)){
        val id = PersonalMessageUtils.getUserGaid()
        this.dataStore.saveUserGaid(id)
        id
    }else{
        gaid
    }

}

suspend fun DataStore<Preferences>.saveToken(value:String){
    this.edit {
        it[keyToken] = value
    }
}

suspend fun DataStore<Preferences>.saveAgreement(Agreement:Boolean){
    this.edit {
        it[agreementKey] = Agreement
    }
}

suspend fun Context.getAgreement():Boolean{
    val tokenFlow :Flow<Boolean> =  this.dataStore.data.map {
        it[agreementKey]?:false
    }
    return tokenFlow.first()
}

suspend fun Context.getToken():String{
   val tokenFlow :Flow<String> =  this.dataStore.data.map {
       it[keyToken]?:""
   }
    return tokenFlow.first()
}

suspend fun DataStore<Preferences>.saveRepostNumber(value: Int){
    this.edit {
        it[keyRepostNumber] = value
    }
}

suspend fun Context.getRepostNumber():Int{
    val repostNumber :Flow<Int> = this.dataStore.data.map {
        it[keyRepostNumber]?:0
    }
    return repostNumber.first()
}

fun Context.startBrowser(url:String):Boolean {
    try {
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
        this.startActivity(intent)
    } catch (e: Exception) {
        return false
    }
    return true
}



